<template>
  <div class="container">
    <el-form
      label-width="80px"
      :model="addSubjectsForm"
      :rules="addSubjectsFormRules"
      ref="addSubjectsFormRef"
    >
      <el-form-item label="学科名称" prop="subjectName">
        <el-input v-model.trim="addSubjectsForm.subjectName"></el-input>
      </el-form-item>

      <el-form-item label="是否显示" prop="isFrontDisplay">
        <el-switch v-model="addSubjectsForm.isFrontDisplay"></el-switch>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close', false)">取 消</el-button>
      <el-button type="primary" @click="addSubjects">确 定</el-button>
    </span>
  </div>
</template>

<script>
// 导入 api
import { add } from '@/api/hmmm/subjects'
export default {
  name: 'SubjectsAdd',
  components: {},
  props: {},
  data () {
    return {
      // 添加学科数据
      addSubjectsForm: {
        subjectName: '',
        isFrontDisplay: false
      },

      addSubjectsFormRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ],
        isFrontDisplay: [
          { required: true, message: '是否显示', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  mounted () {},
  methods: {
    // 添加学科
    addSubjects () {
      this.$refs.addSubjectsFormRef.validate(async (valid) => {
        if (!valid) {
          this.$meaasge('数据验证失败')
        } else {
          try {
            if (this.addSubjectsForm.isFrontDisplay) {
              this.addSubjectsForm.isFrontDisplay = 1
            } else {
              this.addSubjectsForm.isFrontDisplay = 0
            }
            await add(this.addSubjectsForm)
            this.$message.success('添加成功')
            this.$emit('close', false)
            // 更新视图
            this.$emit('update')
          } catch (error) {
            this.$message('添加失败')
          }
        }
      })
    }

  },
  computed: {},
  watch: {},
  filters: {}
}
</script>

<style lang="scss" scoped>
.container {
  .el-input {
    width: 80%;
  }
  .dialog-footer {
    display: flex;
    justify-content: center;
  }
}
</style>
